package com.example.zuul;

import com.google.common.util.concurrent.RateLimiter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;

@Component
public class RateFilter extends ZuulFilter {

    private static final RateLimiter rateLimter = RateLimiter.create(100);

    @Override
    public String filterType() {
        return PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return -5;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        if (!rateLimter.tryAcquire()) {
            System.out.println(" 未拿到令牌 ");
            System.out.println("限流异常 ");
        }else {
            System.out.println(" 拿到令牌 ");
        }

        return null;

    }
}
